/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.chl.angholt.bookproject.db.entity;

import java.util.Calendar;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Patric Peterson
 */
public class Entity {
    
    public Entity() {
    }

    private static EntityManagerFactory emf;
    /**
     * Starting Entity manager factiry befor tests
     * @throws Exception 
     */
    @BeforeClass
    public static void setUpClass() throws Exception {
        emf = Persistence.createEntityManagerFactory("bookproject_pu_test");
    }

    /**
     * closind Entity manager factery befor termanating test
     * @throws Exception 
     */
    @AfterClass
    public static void tearDownClass() throws Exception {
        emf.close();
    }
    
    @Before
    public void setUp() {
    
    }
    
    /**
     * See if there any problems persisting data
     */
    @Test
    public void addingOneTest() {
        Author author = new Author();
        author.setName("dna");
        author.setWebSite("hi there");
        author.setBiography("good writer");
        author.setCountry("uk");
        Book book = new Book();
        book.setTitle("h2g2");
        book.setOriginalLanguage("en");
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(273507582);
        book.setFirstRelease(cal);
        book.setSynopsis("realy good");
        book.setPublisher("Pan Books");
        book.setAuthor(author);
        assertTrue("add failed.", persist(author));
        assertTrue("add failed.", persist(book));
    }
    
    /**
     * persist a object
     * @param obj The object to be persisted
     * @return  if it worked
     */
    public boolean persist(Object obj) {
        boolean result = true;
        try{
            EntityManager em = emf.createEntityManager();
            try{
                EntityTransaction tx = em.getTransaction();
                tx.begin();
                em.persist(obj);
                tx.commit();
            }catch(Exception e){
                result = false;
                System.err.println(e.getMessage());
                e.printStackTrace();
            }finally{
                em.close();
            }
        }catch(Exception e){
            result = false;
            System.err.println(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
    
    /**
     * check if i can ritriv the data add in the last test
     */
    @Test
    public void retrivTest(){
        EntityManager em = emf.createEntityManager();
        Query qu = em.createQuery("SELECT a FROM Author AS a WHERE a.name = 'dna'");
        List<Author> result = ( List<Author> )qu.getResultList();
        assertEquals(result.size(), 1);
        if(result.size() == 1){
            Author author = result.get(0);
            assertEquals(author.getName(),"dna");
            assertEquals(author.getCountry(),"uk");
        }    
    }
    
    /**
     * check that user workes
     */
    @Test
    public void userTest(){
        User user = new User();
        user.setName("pat");
        user.setEmail("pat@fat.com");
        user.setHash(123);
        user.setState("not");
        assertTrue(persist(user));
        
    }
    
    @After
    public void tearDown() {
    }
    // TODO add test methods here.
    // The methods must be annotated with annotation @Test. For example:
    //
    // @Test
    // public void hello() {}
}
